package com.ruoyi.web.service.impl;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.web.service.IWxLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Service
public class WxLoginServiceImpl implements IWxLoginService {

    @Value(value = "${user.password.maxRetryCount}")
    private int maxRetryCount;

    @Value(value = "${user.password.lockTime}")
    private int lockTime;

    @Value("${token.secret}")
    private String secret;

    @Autowired
    private RedisCache redisCache;

    /*@Autowired
    private StaffMapper staffMapper;

    @Override
    public String passwordLogin(WxLoginVo wxLoginVo) {
        System.out.println("wxLoginVo = " + wxLoginVo);
        String redisKey = CacheConstants.PWD_ERR_CNT_KEY + wxLoginVo.getPhone();

        String verifyKey = CacheConstants.CAPTCHA_CODE_KEY + StringUtils.nvl(wxLoginVo.getUuid(), "");
        String captcha = redisCache.getCacheObject(verifyKey);
        redisCache.deleteObject(verifyKey);
        System.out.println("captcha = " + captcha);
        if (captcha == null || !wxLoginVo.getCode().equalsIgnoreCase(captcha)){
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(wxLoginVo.getPhone(), Constants.LOGIN_FAIL, MessageUtils.message("user.jcaptcha.expire")));
            throw new CaptchaException();
        }


        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(wxLoginVo.getPhone(), wxLoginVo.getPassword());
        AuthenticationContextHolder.setContext(authenticationToken);

        Authentication usernamePasswordAuthenticationToken = AuthenticationContextHolder.getContext();
        String phone = usernamePasswordAuthenticationToken.getName();
        String password = usernamePasswordAuthenticationToken.getCredentials().toString();

        Integer retryCount = redisCache.getCacheObject(redisKey);

        if (retryCount == null){
            retryCount = 0;
        }
        if (retryCount >= Integer.valueOf(maxRetryCount).intValue()){
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(wxLoginVo.getPhone(), Constants.LOGIN_FAIL,
                    MessageUtils.message("user.password.retry.limit.exceed", maxRetryCount, lockTime)));
            throw new UserPasswordRetryLimitExceedException(maxRetryCount, lockTime);
        }


        WxLoginVo wxLoginVoT = staffMapper.selectByPhone(wxLoginVo);
        if (!SecurityUtils.matchesPassword(wxLoginVo.getPassword(), wxLoginVoT.getPassword())){
            retryCount = retryCount + 1;
            AsyncManager.me().execute(AsyncFactory.recordLogininfor(wxLoginVo.getPhone(), Constants.LOGIN_FAIL,
                    MessageUtils.message("user.password.retry.limit.count", retryCount)));
            redisCache.setCacheObject(redisKey, retryCount, lockTime, TimeUnit.MINUTES);
            throw new UserPasswordNotMatchException();
        }
        else{
            if (redisCache.hasKey(redisKey)){
                redisCache.deleteObject(redisKey);
            }
        }

        AsyncManager.me().execute(AsyncFactory.recordLogininfor(wxLoginVo.getPhone(), Constants.LOGIN_SUCCESS, MessageUtils.message("user.login.success")));

        // 生成token
        String token = IdUtils.fastUUID();
        Map<String, Object> claims = new HashMap<>();
        claims.put(Constants.LOGIN_USER_KEY, token);
        token = Jwts.builder()
                .setClaims(claims)
                .signWith(SignatureAlgorithm.HS512, secret).compact();

        return token;

    }*/
}
